package com.fzb.forum4j.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fzb.forum4j.dao.api.IBaseDao;
import com.fzb.forum4j.dao.api.IBlogSortDao;
import com.fzb.forum4j.dao.api.IUserDao;
import com.fzb.forum4j.po.Blog;
import com.fzb.forum4j.po.Comment;
import com.fzb.forum4j.po.CommonArea;
import com.fzb.forum4j.po.MessageRecord;
import com.fzb.forum4j.po.QueryMode;
import com.fzb.forum4j.po.User;
import com.fzb.forum4j.service.api.IBlogService;
import com.fzb.forum4j.service.api.IUserService;
import com.fzb.forum4j.util.ParseTools;

@Service("userServiceImpl")

public class UserServiceImpl extends BaseServiceImpl<User> implements
		IUserService {

	
	@Resource(name="hibBaseImpl")
	private IBaseDao<MessageRecord> ibd;
	
	@Resource(name="userDaoImpl")
	private IUserDao userDao;
	@Resource(name="blogSortDaoImpl")
	private IBlogSortDao blogSortDao;
 
	
	
	@Resource(name="blogServiceImpl")
	private IBlogService blogService;
	@Override
	public boolean login(QueryMode<User> t) {
		userDao.login(t);
		if(t.getT()!=null){
			return true;
		}
		return false;
	}
	@Override
	public void getBlogHomeData(QueryMode<User> qmd) {
		QueryMode<Blog> qmdBlog=new QueryMode<Blog>();
		Blog b=new Blog();
		qmdBlog.setRows(qmd.getRows());
		qmdBlog.setPage(qmd.getPage());
		qmdBlog.setT(b);
		qmdBlog.getT().setUser(qmd.getT());
		blogSortDao.getBlogSortByUser(qmd);
		qmd.getData().put("user",userDao.getSimpleDataByUserName(qmd.getT().getUserName()));
		blogService.getBlogsByUser(qmdBlog);
		qmd.getData().putAll(qmdBlog.getData());
	}
	
	
	@Override
	public void getList(QueryMode<User> t) {
		userDao.getList(t);
		
	}
	@Override
	public Boolean existsUserNameAndEmail(User user) {
		return userDao.existsUserNameAndEmail(user);
	}
	@Override
	public User loadUserByUserName(String userName) {
		return userDao.loadUserByUserName(userName);
	}
	@Override
	public void disposeCommentContext(QueryMode<Comment> qmd) {
		
		List<Integer> referers=new ArrayList<Integer>();
		qmd.getT().setContent(ParseTools._GenerateRefererLinks(qmd.getT().getContent(),qmd.getT().getUser(),referers, userDao));
		if(referers!=null){
			
			for (Integer integer : referers) {
			 	User reiceveUser=new User();
			 	reiceveUser.setUserId(integer);
				MessageRecord mr=new MessageRecord(qmd.getT().getUser(),reiceveUser,qmd.getT().getForward(),qmd.getT().getCommTime());
				ibd.addEntity(mr);
			}
		}
		
	}
	@Override
	public void getMessageByUserId(QueryMode<User> qmd) {
		userDao.getMessageByUserId(qmd);
		
	}
	@Override
	public void disposeArticleContext(QueryMode qmd) {
		List<Integer> referers=new ArrayList<Integer>();
		((CommonArea)qmd.getT()).setContent(ParseTools._GenerateRefererLinks(((CommonArea)qmd.getT()).getContent(),((CommonArea)qmd.getT()).getUser(),referers, userDao));
		if(referers!=null){
			if(qmd.getT() instanceof CommonArea){
				qmd=(QueryMode<CommonArea>)qmd;
				for (Integer integer : referers) {
				 	User reiceveUser=new User();
				 	reiceveUser.setUserId(integer);
					MessageRecord mr=new MessageRecord(((CommonArea) qmd.getT()).getUser(),reiceveUser,"xxxx",((CommonArea)qmd.getT()).getPubTime());
					ibd.addEntity(mr);
				}
			}
			
		}
		
	}
	@Override
	public List<Map<String,String>> getHotUser() {
		return userDao.getHotUser();
	}
 


}
